package com.h3bpm.kumhosunny.crm.controller;

import OThinker.Common.Organization.Models.Unit;
import OThinker.Common.Organization.Models.User;
import OThinker.H3.Controller.ControllerBase;
import OThinker.H3.Entity.Notification.Notification;
import OThinker.H3.Entity.Notification.NotifyType;
import com.alibaba.fastjson.JSONObject;
import com.h3bpm.kumhosunny.common.utils.SqlUtil;
import data.DataRow;
import data.DataTable;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @authormi
 * @date2020/1/2
 */

@Component
public class SynchronizationCrmController extends ControllerBase {
    @Override
    public String getFunctionCode() {
        return null;
    }

    public String getAppPerson(String userId){


        Unit user = null;
        String appCode="";
        try {
            user = (User) getEngine().getOrganization().GetUnit(userId);
            if(user!=null) {
                appCode = ((User) user).getCode();
                if (user.getManagerID() != null) {
                    appCode = getParentPeson(user.getObjectID(), appCode);
                } else {
                    appCode = getParentPeson(user.getObjectID(), appCode);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return appCode;

    }


    public String getParentPeson(String unitId,String appCode) throws Exception {
        User unit = (User)   getEngine().getOrganization().GetUnit(unitId);
        Unit unitOrg =    getEngine().getOrganization().GetUnit(unit.getParentID());
        if(unit.getManagerID()!=null && !"".equals(unit.getManagerID()) && !unit.getManagerID().equals(unit.getObjectID())){
            User unitParent =   (User) getEngine().getOrganization().GetUnit(unit.getManagerID());
            appCode  = appCode+";"+unitParent.getCode();
            appCode= getParentPeson( unitParent.getObjectID(),  appCode);
        }else if(unitOrg.getManagerID()!=null && !"".equals(unitOrg.getManagerID()) && !unit.getObjectID().equals(unitOrg.getManagerID())){
            User user = (User)   getEngine().getOrganization().GetUnit(unitOrg.getManagerID());
            appCode=appCode+";"+user.getCode() ;
            appCode=getParentPeson( user.getObjectID(),  appCode);
        }else{
            return appCode;

        }
        return appCode;
    }


    @Scheduled(cron = "0 0 0 * * ?")
    public void synchronizationCrm() {
        SynchronizationCrm(  "CustomerProfile" ,  "appDatas" ,  "salesman");
        SynchronizationCrm(  "ContactFileManager" ,  "appDatas" ,  "exclusiveSalesman");
        SynchronizationCrm(  "sampleApplication" ,  "appDatas" ,  "applicant");
    }


    public void SynchronizationCrm(String bizCode ,String approveFild ,String userFild){
        String sql="select * from i_"+bizCode;

        String updateSql="update i_"+bizCode+"  set  "+approveFild+" =";
        try {

            DataTable dataTable = SqlUtil.doQuery(sql);

            if(dataTable!=null && dataTable.getEntityRows()!=null && dataTable.getEntityRows().size()>0) {
                for (DataRow dataRow : dataTable.getEntityRows()) {
                    if(dataRow.getString(userFild)!=null) {
                        String appCode = getAppPerson(dataRow.getString(userFild));
                        User thisUser = (User)   getEngine().getOrganization().GetUnit(dataRow.getString(userFild));
                        if(thisUser!=null && thisUser.getParentID()!=null) {
                            Unit unit = getEngine().getOrganization().GetUnit(thisUser.getParentID());
                            List<String> userIds = getEngine().getOrganization().GetUsersByJobCode(unit.getObjectID(), "nqPerson", null);
                            if (userIds != null && userIds.size() > 0) {
                                for (String uid : userIds) {
                                    User userNq = (User) getEngine().getOrganization().GetUnit(uid);
                                    appCode = appCode + ";" + userNq.getCode();
                                }
                            }
                        }

                        SqlUtil.doNoQuery(updateSql + "'" + appCode + "' where  ObjectID = '" + dataRow.getString("ObjectID") + "'");
                    }
                }
            }


        } catch (Exception e) {

            e.printStackTrace();
        }
    }





}
